package timesheet.controller;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import timesheet.model.Event;
import timesheet.model.Project;
import timesheet.model.Users;
import timesheet.repository.EventRepository;
import timesheet.repository.ProjectRepository;
import timesheet.repository.UserRepository;
import timesheet.web.ContextUtil;

@Controller
@Transactional
public class TimeSheetController {
	
	@Autowired UserRepository userRepository;
	@Autowired EventRepository eventRepository;
	@Autowired ProjectRepository projectRepository;
	@RequestMapping("/timesheet")
	public ModelAndView timesheet(@RequestParam(value="filteruser", required=false)String[] userfilter, @RequestParam(value="filterproject", required=false)String[] projectfilter){
		Long idUser = (Long)ContextUtil.getHttpSession().getAttribute("UserId");
		if((Long)ContextUtil.getHttpSession().getAttribute("UserId")!=null){
			//Long idUser = (Long)ContextUtil.getHttpSession().getAttribute("USERID_KEY");
			
			//Ask if we just display the action of the user or all actions
			ModelAndView mv = new ModelAndView("article");
			List<Users> userList = userRepository.getAllUser();
			List<Project> projectList = projectRepository.getAllProject();
			List<Event> actionList = new ArrayList<Event>();
			String condition ="";
			if(userfilter!=null){
				condition+= "where e.u in(";
				for(String s : userfilter){
					condition+=s+",";
				}
				condition = condition.substring(0, (condition.length()-1));
				condition+=")";
				//actionList = eventRepository.getAllEventByUserOrProjectList(condition);
			}
			if(projectfilter!=null){
				if(userfilter!=null)
					condition+= " and e.p in(";
				else
					condition+= " where e.p in(";
				for(String s : projectfilter){
					condition+=s+",";
				}
				condition = condition.substring(0, (condition.length()-1));
				condition+=")";
			}
			actionList = eventRepository.getAllEventByUserOrProjectList(condition);
			JSONArray jsonUserList = new JSONArray();
			JSONArray jsonActionList = new JSONArray();
			JSONArray jsonProjectList = new JSONArray();
			JSONArray jsonStatutList = new JSONArray();
			List<String> statutList = new ArrayList<String>();
			statutList.add("Not started");
			statutList.add("In progress");
			statutList.add("Finished");
			try {
				 for(Users u : userList){
					 JSONObject jsonobj = new JSONObject();
					 jsonobj.put("key", u.getId());
					 jsonobj.put("label", u.getPseudo());
					 jsonUserList.put(jsonobj);
					}
				 for(Event a : actionList){
					 JSONObject jsonobj = new JSONObject();
					 jsonobj.put("start_date", a.getStartTime());
					 jsonobj.put("end_date", a.getStopTime());
					 jsonobj.put("text", a.getTask());
					 jsonobj.put("section_id", a.getU().getId());
					 jsonobj.put("project_id", a.getP().getId());
					 jsonobj.put("statut_id", a.getStatut());
					 jsonobj.put("cmt", a.getCommentUser());
					 String hidden = String.valueOf(a.getId());
					 jsonobj.put("hidden",String.valueOf(hidden));
//					 jsonobj.put("restricted", true);
					 jsonActionList.put(jsonobj);
					}
				 for(Project p : projectList){
					 JSONObject jsonobj = new JSONObject();
					 jsonobj.put("key", p.getId());
					 jsonobj.put("label", p.getProjectName());
					 jsonProjectList.put(jsonobj);
					}
				 int i = 1;
				 for(String s : statutList){
					 JSONObject jsonobj = new JSONObject();
					 jsonobj.put("key", i);
					 jsonobj.put("label",s);
					 i++;
					 jsonStatutList.put(jsonobj);
				 }
			} catch (JSONException e) {
				e.printStackTrace();
			}
//			boolean ok = true;
//			mv.addObject("user", user);
			mv.addObject("statutlist", jsonStatutList);
			mv.addObject("userlist", jsonUserList);
			mv.addObject("actionlist", jsonActionList);
			mv.addObject("projectlist", jsonProjectList);
			mv.addObject("ulist",userList);
			mv.addObject("plist",projectList);
//			home.addObject("ok", ok);
			//mv.addObject("caveList", caveList);
			//System.out.println(ContextUtil.getHttpSession().getAttribute(USERID_KEY));
			return mv;
		}
		else{return new ModelAndView("home");}
	}
}